package practice;

import java.io.UnsupportedEncodingException;

public class Demo01 {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = new String("张三");
        // 采用UTF-8编码得到字节数组
        byte[] bytes = str.getBytes("UTF-8");
        // bytes: {e5 8d 8e e4 be a8 e6 b0 b8 e4 ba a8 e9 93 b6 e8 a1 8c}
        // 注意第二个字节0x8d，在下面的编解码中被自动转换成了3f
        System.out.println(str);
        // 采用Windows-1252解码得到字符串
        str = new String(bytes, "Windows-1252");
        // 采用windows-1252编码，还原字节数组
        bytes = str.getBytes("Windows-1252");
        // bytes: {e5 3f 8e e4 be a8 e6 b0 b8 e4 ba a8 e9 93 b6 e8 a1 8c}
        System.out.println(str);
        // 采用UTF-8解码，预期得到原字符串
        str = new String(bytes, "UTF-8");
        // str: ???侨永亨银行,第一个字被永久性破坏
        System.out.println(str);
    }
}
